package net.lcheng.commons.taglib;

import com.alibaba.druid.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * Created by qiliping on 15/11/14.
 * 自定义分页标签
 * (使用时请手动拷贝webapp/WEB-INF/paging-tablib.tld到网站对应的目录)
 */
public class PagingTag extends SimpleTagSupport {

    private String href;

    //分页的参数名
    private String currentPageName;

    private String htmlTag;

    private int pageIndex;//当前页

//    private int size;//每页条数

    private int totalPage;//总页数

    //激活样式
    private String activeClass;

    //禁用效果
    private String disabledClass;


    @Override
    public void doTag() throws JspException, IOException {
        if (StringUtils.isEmpty(currentPageName)) {
            currentPageName = "page";
        }
        if(StringUtils.isEmpty(activeClass))
        {
            activeClass = "active";
        }
        if(StringUtils.isEmpty(disabledClass))
        {
            disabledClass = "disabled";
        }
        if(totalPage <2){
            return;
        }
        PageContext pc = (PageContext) getJspContext();
        HttpServletRequest request = (HttpServletRequest) pc.getRequest();

        Enumeration<String> paraNames = request.getParameterNames();

        /*System.out.println("当前链接:"+href);

        System.out.println("当前根目录:"+request.getContextPath());
        System.out.println("当前根路径:"+request.getServletPath());
        System.out.println("当前根路径2:"+request.getRequestURI());
        System.out.println("当前根路径3:"+request.getRequestURL().toString().replace(request.getRequestURI(),""));
        System.out.println("当前根路径4:"+request.getRequestURL().toString());
        System.out.println("当前根路径5:"+request.getServletContext().getServletContextName());*/

        href += "?";
        for(Enumeration e=paraNames;e.hasMoreElements();){

            String thisName=e.nextElement().toString();
            String thisValue=request.getParameter(thisName);
            if(currentPageName.equals(thisName) == false) {
                href += (thisName + "=" + thisValue + "&");
                System.out.println(thisName + "--------------" + thisValue);
            }
        }




        String urlStr = request.getRequestURL().toString();
        URL url = new URL(urlStr);

        JspWriter out = getJspContext().getOut();



        if (!href.endsWith("?") && !href.endsWith("&")) {
            if (href.indexOf("?") == -1) {
                href = href + "?";
            } else {
                href = href + "&";
            }
        }

        if (pageIndex <= 0) {
            pageIndex = 1;
        } else if (pageIndex > totalPage) {
            pageIndex = totalPage;
        }

//        out.append("<span>");
        // 首页
        if (pageIndex == 1) {
//            out.append("首页");
            href(out, "#", 1, "首页",disabledClass);
        } else {
            href(out, href, 1, "首页","");
        }
//        out.append(" | ");
        // 上一页
        if (pageIndex == 1) {
//            out.append("上一页");
            href(out, "#", pageIndex - 1, "上一页",disabledClass);
        } else {
            href(out, href, pageIndex - 1, "上一页","");
        }

        /**
         * 中间的页码
         * */
        if(totalPage<=5)
        {
            for (int i =1 ;i<=totalPage;i++)
            {
                if (pageIndex == i) {
                    href(out, "#", i, String.valueOf(i),activeClass);
                } else {
                    href(out, href, i, String.valueOf(i),"");
                }
            }
        }else
        {
            if(pageIndex>2)
            {
                for (int i =pageIndex-2;i<=pageIndex+2;i++){
                    if (pageIndex == i) {
                        href(out, "#", i, String.valueOf(i),activeClass);
                    } else {
                        if(i<=totalPage) {
                            href(out, href, i, String.valueOf(i), "");
                        }
                    }
                }
            }
            else
            {
                for (int i =1;i<=5;i++){
                    if (pageIndex == i) {
                        href(out, "#", i, String.valueOf(i),activeClass);
                    } else {
                        href(out, href, i, String.valueOf(i),"");
                    }
                }
            }
        }

//        out.append(" | ");
        // 下一页
        if (pageIndex == totalPage) {
//            out.append("下一页");
            href(out, "#", pageIndex + 1, "下一页",disabledClass);
        } else {
            href(out, href, pageIndex + 1, "下一页","");
        }
//
        // 末页
        if (pageIndex == totalPage) {
//            out.append("末页");
            href(out, "#", pageIndex + 1, "末页",disabledClass);
        } else {
            href(out, href, totalPage, "末页","");
        }
//
        out.append("<li class='"+disabledClass+"'><a>第");
        out.append(pageIndex + "/" + totalPage);
        out.append("页</a></li>");


        super.doTag();
    }

    private void href(JspWriter out, String href, int curr, String title,String className) throws IOException {
        if (htmlTag.equals("") == false) {
            out.append("<");
            out.append(htmlTag);
            if(StringUtils.isEmpty(className)==false)
            {
                out.append(" class='"+className+"'>");
            }else {
                out.append(">");
            }
        }
        if (href.equals("#")) {
            out.append("<a href='javascript:void(0);'>")
                    .append(title)
                    .append("</a>");

        } else {
            out.append("<a href=\"")
                    .append(href)
                    .append(currentPageName)
                    .append("=")
                    .append("" + curr)
                    .append("\">")
                    .append(title)
                    .append("</a>");
        }

        if (htmlTag.equals("") == false) {
            out.append("</");
            out.append(htmlTag);
            out.append(">");
        }
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getCurrentPageName() {
        return currentPageName;
    }

    public void setCurrentPageName(String currentPageName) {
        this.currentPageName = currentPageName;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public String getHtmlTag() {
        return htmlTag;
    }

    public void setHtmlTag(String htmlTag) {
        this.htmlTag = htmlTag;
    }

//    public int getSize() {
//        return size;
//    }
//
//    public void setSize(int size) {
//        this.size = size;
//    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
}
